package logic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Answer implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4720341311347732864L;
	private String id;
	private String text;
	private boolean selected = false;
	private String questionId;

	/* Data base fields */
	public static final String KEY_ID = "id";
	public static final String KEY_QUESTION_ID = "question_id";
	public static final String KEY_TEXT = "text";

	public Answer(String text) {
		this.text = text;
	}

	public Answer(String id, String text, String questionId) {
		this.id = id;
		this.text = text;
		this.questionId = questionId;
	}
	
	public String getId() {
		return id;
	}

	public String getText() {
		return text;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public boolean isSelected() {
		return this.selected;
	}

	public static List<Answer> getAnswersFromJSON(String json) {
		ArrayList<Answer> answers = new ArrayList<Answer>();
		try {
			// JSONObject Object = new JSONObject(JSON);
			JSONArray jsonArray = new JSONArray(json);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String id = jsonObject.getString("id_respuesta");
				String text = jsonObject.getString("texto");
				String idPregunta = jsonObject.getString("id_pregunta");

				answers.add(new Answer(id, text, idPregunta));
			}
			return answers;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
}
